package com.tuanchau.android.englishtooltip.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.tuanchau.android.englishtooltip.Global;
import com.tuanchau.android.englishtooltip.R;

public class SettingActivity extends Activity implements OnClickListener {

	private TextView txtDelayValue;
	private Spinner spnPlayOrder;
	private SeekBar seekVolume;
	private ToggleButton showToastBtn;
	private SharedPreferences settingPref;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		initView();
	}

	private void initView() {
		Button btnDelayDecrease = (Button) findViewById(R.id.activity_setting_delay_decrease);
		Button btnDelayIncrease = (Button) findViewById(R.id.activity_setting_delay_increase);
		Button btnApply = (Button) findViewById(R.id.activity_setting_apply);
		Button btnCancel = (Button) findViewById(R.id.activity_setting_cancel);
		showToastBtn = (ToggleButton) findViewById(R.id.activity_setting_show_toast);
		spnPlayOrder = (Spinner) findViewById(R.id.activity_setting_order);
		txtDelayValue = (TextView) findViewById(R.id.activity_setting_delay_value_text);
		seekVolume = (SeekBar) findViewById(R.id.activity_setting_volume);

		btnApply.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		btnDelayDecrease.setOnClickListener(this);
		btnDelayIncrease.setOnClickListener(this);

		settingPref = getSharedPreferences(Global.PREF_FILE_NAME, Context.MODE_PRIVATE);
		int delayValue = settingPref.getInt(Global.PREF_KEY_DELAY, 5);
		int playOrder = settingPref.getInt(Global.PREF_KEY_PLAY_ORDER, 0);
		int volume = settingPref.getInt(Global.PREF_KEY_VOLUME, 8);
		boolean isShowToast = settingPref.getBoolean(Global.PREF_KEY_SHOW_TOAST, false);
		txtDelayValue.setText("" + delayValue);
		spnPlayOrder.setSelection(playOrder);
		seekVolume.setProgress(volume);
		showToastBtn.setChecked(isShowToast);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int vId = v.getId();
		switch (vId) {
		case R.id.activity_setting_delay_decrease:
			onDecreaseClicked();
			break;
		case R.id.activity_setting_delay_increase:
			onIncreaseClicked();
			break;

		case R.id.activity_setting_apply:
			onApplyClicked();
			break;
		case R.id.activity_setting_cancel:
			onCancelClicked();
			break;

		default:
			break;
		}

	}

	private void onDecreaseClicked() {
		String str = txtDelayValue.getText().toString();
		int value = Integer.parseInt(str);
		if (value > 2) {
			value--;
			txtDelayValue.setText("" + value);
		}
	}

	private void onIncreaseClicked() {
		String str = txtDelayValue.getText().toString();
		int value = Integer.parseInt(str);
		value++;
		txtDelayValue.setText("" + value);
	}

	private void onApplyClicked() {
		String str = txtDelayValue.getText().toString();
		int delayValue = Integer.parseInt(str);
		int playOrder = (int) spnPlayOrder.getSelectedItemId();
		int volume = seekVolume.getProgress();
		Editor settingEditor = settingPref.edit();
		settingEditor.putInt(Global.PREF_KEY_DELAY, delayValue);
		settingEditor.putInt(Global.PREF_KEY_PLAY_ORDER, playOrder);
		settingEditor.putInt(Global.PREF_KEY_VOLUME, volume);
		settingEditor.putBoolean(Global.PREF_KEY_UPDATE_SETTING, true);
		settingEditor.putBoolean(Global.PREF_KEY_SHOW_TOAST, showToastBtn.isChecked());
		settingEditor.commit();
		finish();
	}

	private void onCancelClicked() {
		finish();
	}

}
